/*
 * Filename CheckDataRepositoryImpl.java 2021年09月27日
 * Copyright © Ehome Co.Ltd. All Rgiths Reserved.
 * @author wuyingchao
 */
package com.ehome.poct.core.dao.mybatis;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ehome.poct.core.api.dto.check.CellCheckDataDTO;
import com.ehome.poct.core.api.dto.check.CheckStatus;
import com.ehome.poct.core.dao.dataconverter.CellCheckDataConverter;
import com.ehome.poct.core.dao.entity.check.CheckDataDO;
import com.ehome.poct.core.dao.mybatis.mapper.CheckDataMapper;
import com.ehome.poct.core.repository.CellCheckDataRepository;
import lombok.AllArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 实现
 *
 * @author wyc
 * @since 2021/09/27 11:31
 **/
@Repository
@AllArgsConstructor
public class CellCheckDataRepositoryImpl implements CellCheckDataRepository {
    private final CheckDataMapper checkDataMapper;
    private final RedisTemplate redisTemplate;


    @Override
    public CheckStatus findCacheById(Long dataId) {
        final var obj = redisTemplate.opsForValue().get("blood_check_status:" + dataId);
        if (ObjectUtil.isNotNull(obj)) {
            return new JSONObject(obj).toBean(CheckStatus.class);
        }
        final var data = checkDataMapper.selectById(dataId);
        return new CheckStatus(data.getCheckStatus(), data.getAbnormalMessage());
    }

    @Override
    public CellCheckDataDTO findById(Long id) {
        return toCheckData(checkDataMapper.selectById(id));
    }

    private static CellCheckDataDTO toCheckData(CheckDataDO entity) {
        final var data = new CellCheckDataDTO(entity.getAlgoReqId(), entity.getTermClinicId(), entity.getPatientId(),
                entity.getCheckType(), entity.getSno(), entity.getBoardId(),
                entity.getCheckStatus(), entity.getOssFileId(),entity.getCheckResultImgs());
        data.setId(entity.getBloodCheckDataId());
        data.setResult(entity.getCheckResult());
        data.setChart(entity.getCheckResultChatData());
        data.setCheckTime(entity.getCreateTime());
        data.setAbnormalMsg(entity.getAbnormalMessage());
        return data;
    }

    @Override
    public void save(CellCheckDataDTO checkData) {
        final var dataDO = CellCheckDataConverter.toCheckDataDO(checkData);
        checkDataMapper.insert(dataDO);
        redisTemplate.opsForValue().set("blood_check_status:" + dataDO.getBloodCheckDataId(),
                new CheckStatus(dataDO.getCheckStatus(), "检测中"), 120, TimeUnit.MINUTES);
        checkData.setId(dataDO.getBloodCheckDataId());
    }

    @Override
    public boolean updateResult(Long reqId, List<String> msg, String result, String morphology, String chart, String images, Boolean hasZero) {
        final var data = checkDataMapper.selectOne(Wrappers.<CheckDataDO>lambdaQuery().eq(CheckDataDO::getAlgoReqId, reqId));
        final var checkStatus = new CheckStatus(2, "检测完成");
        if (CollUtil.isNotEmpty(msg)) {
            checkStatus.setStatus(hasZero ? 4 : 3).setMsg(String.join("\n", msg));
        }
        final var success = data
                .setCheckResult(result)
                .setCheckResultChatData(chart)
                .setCheckResultMorphology(morphology)
                .setCheckResultImgs(images)
                .setCheckStatus(checkStatus.getStatus())
                .setAbnormalMessage(checkStatus.getMsg())
                .updateById();
        if (success) {
            redisTemplate.opsForValue().set("blood_check_status:" + data.getBloodCheckDataId(), checkStatus, 120, TimeUnit.MINUTES);
        }
        return success;
    }
}